﻿#include "imagesourcecameraoperate.h"

ImageSourceCameraOperate::ImageSourceCameraOperate()
{

}

void ImageSourceCameraOperate::Init(Grabber *m_cGrabber)
{
    this->m_cGrabber = m_cGrabber;
}

void ImageSourceCameraOperate::setDisplayParameter(HWND hwnd, int width, int height)
{
    //设置展示窗口
    m_cGrabber->setHWND(hwnd);
    m_cGrabber->setDefaultWindowPosition(false);
    m_cGrabber->setWindowSize(width, height);
}

void ImageSourceCameraOperate::display()
{
    //开始展示
    if(m_cGrabber->isDevValid())
    {
        m_cGrabber->startLive();
    }
}

void ImageSourceCameraOperate::showDevicePage()
{
    //显示配置界面,一般用于第一次展示配置页面
    bool isLive = m_cGrabber->isLive();
    if(m_cGrabber->isDevValid() && isLive)
    {
        m_cGrabber->stopLive();
    }

    m_cGrabber->showDevicePage();
    if(isLive)//如果原来展示着呢，就展示。否则不展示
    {
        if(m_cGrabber->isDevValid())
        {
            m_cGrabber->startLive();
        }
    }
}

void ImageSourceCameraOperate::showPropertiesPage()
{
    if( m_cGrabber->isDevValid())
    {
        m_cGrabber->showVCDPropertyPage();
    }
}

void ImageSourceCameraOperate::saveDeviceStateToFile(QString fileName)
{
    if( m_cGrabber->isDevValid())
    {
        m_cGrabber->saveDeviceStateToFile(fileName.toStdString());
    }
}

void ImageSourceCameraOperate::loadDeviceStateFromFile(QString fileName)
{
    bool isLive = m_cGrabber->isLive();
    if(m_cGrabber->isDevValid() && isLive)
    {
        m_cGrabber->stopLive();
    }

    if( m_cGrabber->loadDeviceStateFromFile(fileName.toStdString()))
    {
        if(isLive)
        {
            if( m_cGrabber->isDevValid())
                m_cGrabber->startLive();
        }
    }
}
tFrameQueueBufferPtr ImageSourceCameraOperate::getSingleFrame( Grabber& gr, const GUID& media_type )
{
    smart_ptr<FrameSnapSink> p_sink = FrameSnapSink::create( FrameTypeInfo( media_type ) );
    gr.setSinkType( p_sink );
    gr.startLive( false );
    Sleep( 250 ); // give the device time to adjust automatic settings i.e. auto exposure
    tFrameQueueBufferPtr img_ptr = p_sink->snapSingle( 2000 );
    gr.stopLive();
    gr.setSinkType( nullptr );
    // get pointer to the image data
    return img_ptr;
}
void ImageSourceCameraOperate::getSingleImg(tFrameQueueBufferPtr * pActiveBuf)
{
    m_cGrabber->setOverlayBitmapPathPosition( ePP_NONE );
    bool isLive = m_cGrabber->isLive();
    if(m_cGrabber->isDevValid() && isLive)
    {
        m_cGrabber->stopLive();
    }
    //16进制的图像有问题。强行转。
    GUID subtype = MEDIASUBTYPE_Y16;
    if(subtype == m_cGrabber->getVideoFormat().getSubtype()){
        subtype = MEDIASUBTYPE_Y800;
    }else{
        subtype = m_cGrabber->getVideoFormat().getSubtype();
    }
    *pActiveBuf = getSingleFrame( *m_cGrabber, subtype);//MEDIASUBTYPE_Y800
    if(isLive)
    {
        m_cGrabber->startLive();
    }

}

bool ImageSourceCameraOperate::getSingleImg2Mat(cv::Mat * mat)
{
    if(m_cGrabber->isDevValid() && m_cGrabber->isLive())
    {
        int nH = m_cGrabber->getVideoFormat().getSize().cy;
        int nW = m_cGrabber->getVideoFormat().getSize().cx;
        QString colorformat = QString::fromStdString(m_cGrabber->getVideoFormat().getColorformatString());
        int nFlag = 1;//Y800
        int nType =  CV_8UC1 ;//Y800
        if(colorformat.contains("RGB")){
            nFlag = 3;//RGB24
            nType = CV_8UC3 ;//RGB24
        }
        int nByte = nH * nW * nFlag ;//字节计算
        *mat = cv::Mat::zeros(nH, nW, nType);
        tFrameQueueBufferPtr ptr;
        getSingleImg(&ptr);
        BYTE* pbImgData = ptr->getPtr();
        memcpy(mat->data, pbImgData, nByte);
        if(colorformat.contains("RGB")){
            cv::flip(*mat, *mat, 0);//RGB24
        }
        return true;
    }
    return false;
}



